package lumis.service.portalmanagement.channeltemplate;

import lumis.doui.table.TableUpdateDataProcessActionHandler;
import lumis.portal.PortalException;
import lumis.portal.channel.template.ChannelTemplateUpdateChannelsRunner;
import lumis.portal.manager.ManagerFactory;
import lumis.util.ITransaction;
import lumis.util.PortalUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

/**
 * Process Action Handler to update channels from template.
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class UpdateChannelsProcessActionHandler extends TableUpdateDataProcessActionHandler
{
	@Override
	public void processAction() throws PortalException
	{
		String[] channelIds = (String[])getParameter("channelId");

		if ((channelIds != null) && (channelIds.length != 0))
		{
			String processId = PortalUtil.generateNewGuid();
			setParameter("processId", processId);

			beginUpdateChannelsStructureFromTemplate(sessionConfig.getUserId(), sessionConfig.getLocale(), processId,
					Arrays.asList(channelIds), transaction);
			
		}
		else
		{
			throw new PortalException("STR_NO_ITEM_SELECTED");
		}

		addDefaultResponse();
	}

	/**
	 * @since 6.0.0
	 */
	private void beginUpdateChannelsStructureFromTemplate(String userId, Locale locale, String processId,
			List<String> channelIds,
			ITransaction transaction)
	{
		ManagerFactory.getExecutorManager().submit(
				new ChannelTemplateUpdateChannelsRunner(userId, locale, processId, channelIds));
	}
}
